package com.shuaigef.springbootinit.utils;

import com.alibaba.fastjson.JSONObject;
import com.shuaigef.springbootinit.domain.Catalog;
import com.shuaigef.springbootinit.domain.req.CoverReq;
import com.shuaigef.springbootinit.domain.req.CreateReq;
import com.shuaigef.springbootinit.domain.req.StructureReq;
import com.shuaigef.springbootinit.domain.res.CoverRes;
import com.shuaigef.springbootinit.domain.res.PPTRes;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static com.shuaigef.springbootinit.bean.HttpBean.jsonMediaType;

@Component
public class PPTUtils {
    private static String token;

    private static OkHttpClient client;

    /**
     * 获取PPT大纲
     * @param req
     * @return PPT大纲
     */
    public static List<Catalog> createStructure(StructureReq req){
        RequestBody body = RequestBody.create(jsonMediaType, JSONObject.toJSONString(req));
        System.out.println(token);
        Request request = new Request.Builder()
                .url("https://saas.api.yoo-ai.com/apps/ppt-structure")
                .addHeader("Authorization", token)
                .post(body)
                .build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            return jsonObject.getObject("data", JSONObject.class).getJSONArray("catalogs").toJavaList(Catalog.class);
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        return null;
    }

    /**
     * 创建PPT封面
     * @param req
     * @return 封面列表
     */
    public static List<CoverRes> createCover(CoverReq req){
        RequestBody body = RequestBody.create(jsonMediaType, JSONObject.toJSONString(req));
        Request request = new Request.Builder()
                .url("https://saas.api.yoo-ai.com/apps/ppt-cover")
                .addHeader("Authorization", token)
                .post(body)
                .build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            return jsonObject.getJSONArray("data").toJavaList(CoverRes.class);
        }catch (IOException e){
            System.err.println(e.getMessage());
        }
        return null;
    }

    /**
     * 创建PPT任务
     * @param req
     * @return PPT任务ID
     */
    public static String createPPT(CreateReq req){
        System.out.println(req);
        RequestBody body = RequestBody.create(jsonMediaType, JSONObject.toJSONString(req));
        Request request = new Request.Builder()
                .url("https://saas.api.yoo-ai.com/apps/ppt-create")
                .addHeader("Authorization", token)
                .post(body)
                .build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            return jsonObject.getObject("data", JSONObject.class).getString("id");
        } catch (IOException e){
            System.err.println(e.getMessage());
        }
        return null;
    }

    /**
     * 获取PPT任务生成状态
     * @param id
     * @return PPT任务状态
     */
    public static PPTRes getResult(String id){
        Request request = new Request.Builder()
                .url("https://saas.api.yoo-ai.com/apps/ppt-result?id="+id)
                .addHeader("Authorization", token)
                .get()
                .build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            return jsonObject.getObject("data", PPTRes.class);
        } catch (IOException e){
            System.err.println(e.getMessage());
        }
        return null;
    }

    /**
     * 下载PPT
     * @param id
     * @return PPT下载链接
     */
    public static String downloadPPT(String id){
        Request request = new Request.Builder()
            .url("https://saas.api.yoo-ai.com/apps/ppt-download?id="+id)
            .addHeader("Authorization", token)
            .get()
            .build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            return JSONObject.parseObject(response.body().string()).getObject("data", JSONObject.class).getString("download_url");
        } catch (IOException e){
            System.err.println(e.getMessage());
        }
        return null;
    }


    @Resource
    public void setClient(OkHttpClient okHttpClient){
        PPTUtils.client = okHttpClient;
    }

    @Value("${weinike.token}")
    public void setToken(String _token){
        PPTUtils.token = _token;
    }
}
